package b.sync.sample

import b.sync.domain.{MySQL, QuillHelper, Replicator}
import cats.effect
import eu.timepit.refined.auto._
import shapeless._ // For macros

object TPckReplicator extends Replicator[TPckRow, TPckRow, TKey] with QuillHelper {

  import quillCtx._

  private val sample = TPckRow(0, t = new java.util.Date())
  private val table = quote(querySchema[TPckRow]("pck_dup_1"))
  private val key = quote(table.filter(p => p.id == lift(0L)))
  override val stmtInsert = quillCtx.run(quote(table.insert(lift(sample)))).string
  override val stmtUpdate = quillCtx.run(quote(key.update(lift(sample)))).string
  override val stmtDelete = quillCtx.run(quote(key.delete)).string
  override val ds = MySQL("testdup", "root", "root")

  override def replicateOne(rowsGroup: (Int, List[(TPckRow, Int)])): effect.IO[Int] =
    macroReplicate[TPckRow, TKey]

  override def copyTo(a: TPckRow, opType: Index): (TPckRow, Int) =
    (a.copy(a.id + 1000, v = a.v.map(_ + "!!!")), opType)

  override def uniqueKeyOf(row: TPckRow): TKey = TKey(row.id)
}
